package com.runda.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.bbjob.dto.Result;
import com.bbjob.mapper.ExamInfoMapper;
import com.bbjob.mapper.ExamineeInfoMapper;
import com.bbjob.mapper.SysOrganizationMapper;
import com.bbjob.mapper.SysOrganizationStuMapper;
import com.bbjob.model.ExamInfo;
import com.bbjob.model.ExamineeInfo;
import com.bbjob.model.SysOrganization;
import com.bbjob.model.SysOrganizationStu;
import com.bbjob.util.service.impl.BaseService;
import com.github.pagehelper.PageHelper;
import com.runda.query.QueryExaminee;
import com.runda.service.IExamInfoService;
import com.runda.service.IExamineeInfoService;

@Service
public class ExamineeInfoServiceImpl extends BaseService<ExamineeInfo> implements IExamineeInfoService {
	
	@Resource
	private SysOrganizationMapper sysOrganizationMapper;
	
	@Resource
	private ExamineeInfoMapper examineeInfoMapper;
	
	@Resource
	private ExamInfoMapper examInfoMapper;
	
	@Resource
	private SysOrganizationStuMapper sysOrganizationStuMapper;
	
	@Resource
	private IExamInfoService examInfoService;

	@Override
	public List<ExamineeInfo> selectByParams(QueryExaminee examinee) {

		if (examinee.getUniquecode() != null && !"".equals(examinee.getUniquecode())) {
			Example organExample = new Example(SysOrganization.class);
			organExample.createCriteria().andEqualTo("uniqueCode", examinee.getUniquecode());
			List<SysOrganization> organs = sysOrganizationMapper.selectByExample(organExample);
			
			if (organs.size() > 0) {
				examinee.setOrganSchool(organs.get(0).getId());
			}
		}
		
		PageHelper.startPage(examinee.getPageNum(), examinee.getPageSize());
		
		Example example = new Example(ExamineeInfo.class);
		Criteria c = example.createCriteria();
		

		if (examinee.getSno() != null && !"".equals(examinee.getSno())) {
			c.andLike("sNo", "%" + examinee.getSno() + "%");
		}

		if (examinee.getSname() != null && !"".equals(examinee.getSname())) {
			c.andLike("field1", "%" + examinee.getSname() + "%");
		}
		
		if (examinee.getOrganSchool() != null) {
			c.andEqualTo("organId", examinee.getOrganSchool());
		}
		
		if (examinee.geteId() != null) {
			c.andEqualTo("eId", examinee.geteId());
		}
		
		List<ExamineeInfo> list = examineeInfoMapper.selectByExample(example);
		
		return list;
	}

	@Override
	public Result entry(ExamineeInfo examineeInfo) {
		if (examineeInfo.getUniquecode() != null && !"".equals(examineeInfo.getUniquecode())) {
			Example example = new Example(SysOrganization.class);
			example.createCriteria().andEqualTo("uniqueCode", examineeInfo.getUniquecode());
			List<SysOrganization> organs = sysOrganizationMapper.selectByExample(example);
			if (organs.size() > 0) {
				examineeInfo.setOrganId(organs.get(0).getId());
			} else {
				return new Result(8106, "新增失败，无法获取学校信息");
			}
			
			ExamInfo examInfoCurr = examInfoMapper.selectByPrimaryKey(examineeInfo.geteId());
			
			// 判断学生信息表中是否有记录
			Example stuExample = new Example(SysOrganizationStu.class);
			stuExample.createCriteria().andEqualTo("organSchool", examineeInfo.getOrganId()).andEqualTo("sno", examineeInfo.getsNo()).andEqualTo("sname", examineeInfo.getField1());
			List<SysOrganizationStu> stus = sysOrganizationStuMapper.selectByExample(stuExample);
			if (stus.size() == 0) {
				return new Result(8106, "新增失败，未查询到该学生信息");
			}
			
			Example sExample = new Example(ExamineeInfo.class);
			sExample.createCriteria().andEqualTo("eId", examineeInfo.geteId()).andEqualTo("organId", examineeInfo.getOrganId()).andEqualTo("eNo", examineeInfo.geteNo());
			List<ExamineeInfo> examineeInfoEno = examineeInfoMapper.selectByExample(sExample);
			if (examineeInfoEno.size() > 0) {
				return new Result(8106, "新增失败，该考号已在此次考试中注册过");
			}
			
			sExample.clear();
			sExample.createCriteria().andEqualTo("sNo", examineeInfo.getsNo()).andEqualTo("field1", examineeInfo.getField1()).andEqualTo("organId", examineeInfo.getOrganId()).andEqualTo("eNo", examineeInfo.geteNo());
			List<ExamineeInfo> examineeInfoSno = examineeInfoMapper.selectByExample(sExample);
			for (ExamineeInfo info : examineeInfoSno) {
				ExamInfo examInfoHis = examInfoMapper.selectByPrimaryKey(info.geteId());
				Long hisEndTime = examInfoHis.geteEndtime().getTime();
				Long currStartTime = examInfoCurr.geteStarttime().getTime();
				if (hisEndTime > currStartTime) {
					return new Result(8106, "新增失败，该考生有一场未结束的考试，无法参加此场考试");
				}
			}
			
/*			sExample.clear();
			sExample.createCriteria().andEqualTo("organId", examineeInfo.getOrganId()).andEqualTo("eNo", examineeInfo.geteNo()).andEqualTo("sNo", examineeInfo.getsNo());
			List<ExamineeInfo> examineeInfoSno = examineeInfoMapper.selectByExample(sExample);
			if (examineeInfoSno.size() > 0) {
				return new Result(8106, "新增失败，该考生和考号已在其他考试中使用过");
			}*/
			
			sExample.clear();
			sExample.createCriteria().andEqualTo("eId", examineeInfo.geteId()).andEqualTo("organId", examineeInfo.getOrganId()).andEqualTo("sNo", examineeInfo.getsNo());
			List<ExamineeInfo> examineeInfos = examineeInfoMapper.selectByExample(sExample);
			
			if (examineeInfos.size() > 0) {
				examineeInfo.setUpdateTime(new Date());
				examineeInfo.setId(examineeInfos.get(0).getId());
				this.updateAll(examineeInfo);
			} else {
				examineeInfo.setCreateTime(new Date());
				examineeInfo.setUpdateTime(new Date());
				this.save(examineeInfo);
			}
			
			// 重新分配试卷
			try {
				examInfoService.allocatePaper(examineeInfo.getUniquecode(), examineeInfo.geteId().toString());
			} catch (Exception e) {
				e.printStackTrace();
				return new Result(8106, "重新分配试卷出错");
			}
			
			Result result = new Result(200, "保存成功");
			result.setData(examineeInfo);
			return result;
		} else {
			return new Result(8106, "学校唯一编码为空，无法查询学校信息");
		}
	}

	@Override
	public Result update(ExamineeInfo examineeInfo) {
		
		ExamineeInfo ori = examineeInfoMapper.selectByPrimaryKey(examineeInfo.getId());
		
		Example sExample = new Example(ExamineeInfo.class);
		sExample.createCriteria().andEqualTo("eId", ori.geteId()).andEqualTo("organId", ori.getOrganId()).andEqualTo("eNo", examineeInfo.geteNo());
		List<ExamineeInfo> examineeInfoEno = examineeInfoMapper.selectByExample(sExample);
		if (examineeInfoEno.size() > 0) {
			return new Result(8106, "新增失败，该考号已注册");
		} else {
			
			this.updateNotNull(examineeInfo);
			return new Result();
		}
	}

	@Override
	public Result updateDistByEno(ExamineeInfo examineeInfo) {
		ExamineeInfo compare = examineeInfoMapper.selectByPrimaryKey(examineeInfo.getId());
		
		if (compare.geteNo().equals(examineeInfo.geteNo())) {
			
			return new Result();
		} else {
			Example sExample = new Example(ExamineeInfo.class);
			sExample.createCriteria().andEqualTo("eId", compare.geteId()).andEqualTo("organId", compare.getOrganId()).andEqualTo("eNo", examineeInfo.geteNo());
			List<ExamineeInfo> examineeInfoEno = examineeInfoMapper.selectByExample(sExample);
			if (examineeInfoEno.size() > 0) {
				return new Result(8106, "修改失败，该考号已在此次考试中注册过");
			}

			examineeInfoMapper.updateByPrimaryKeySelective(examineeInfo);
		}
		
/*		sExample.clear();
		sExample.createCriteria().andEqualTo("organId", compare.getOrganId()).andEqualTo("eNo", examineeInfo.geteNo()).andEqualTo("sNo", compare.getsNo());
		List<ExamineeInfo> examineeInfoSno = examineeInfoMapper.selectByExample(sExample);
		if (examineeInfoSno.size() > 0) {
			return new Result(8106, "新增失败，该考生和考号已在其他考试中使用过");
		}*/
	
		return new Result();
	}
}
